$(function(){

    // 歌曲播放功能

    function musicPlay(){
        // console.log("播放");

       var audio = $("#audio")[0];
       if(audio.paused){
        audio.play();
        $("#playImg").attr("src", "./images/pause.png");
       }else{
        audio.pause();
        $("#playImg").attr("src", "./images/play-btn_03.png");
       }

        //    播放时间与总时间显示
        // 定时器控制器
        var playTimer = null;
        playTimer = setInterval(function(){
            if(audio.paused){
                clearInterval(playTimer);
            }
            // 获得当前时间
            var playTime_s = audio.currentTime;
            console.log(playTime_s);
            
            var mTime = parseInt(playTime_s / 60);

            var sTime = parseInt(playTime_s % 60);

            // 获取总时长

            var totalTime_s = audio.duration;
            
            var mTine_total = parseInt(totalTime_s / 60);

            var sTime_total = parseInt(totalTime_s % 60);

            function checkTime(m, s){
                if(m < 10){
                    m = "0" + m;
                }else{
                    m += "";
                }
                if(s < 10){
                    s = "0" + s;
                }else {
                    s += "";
                } 
                return m + ":" +  s;
            }

            var pagePassTime = checkTime(mTime, sTime);

            var pageTotalTime = checkTime(mTine_total, sTime_total);

            $("#passTime").html(pagePassTime);
            $("#totalTime").html(pageTotalTime);

            // 进度条

            var palyedTimePercent = playTime_s / totalTime_s;

            if(totalTime_s - playTime_s > 0){
                $(".pogress-bar-passed").width(palyedTimePercent * 100 + "%");
            }else{
                $(".pogress-bar-passed").width(0 + "%");
            }

       }, 1000)
    }

    $("#playBtn").on("click", function(){
        musicPlay();
    })



    // 获取文章内容
    $.ajax({
        type:'get',
        url:"http://localhost/blueberrypai_server/page_yuezhang.php",
        success:function(data){
            data = JSON.parse(data);
            // console.log(data);
            $(".blog-details-content").html(decodeURI(data.artical.artical_cont));
            $(".artical-title-h3").html(data.artical.artical_title);

        }
    })

    // 获取乐章页面的相关阅读数据
    $.ajax({
        type:"get",
        url:"http://localhost/blueberrypai_server/page_yuezhang_read.php",
        success: function(data){
            data = JSON.parse(data);
            console.log(data);
            $(data.readList).map(function(index, item){
                $(".related-read-content").append("\
                    <div class='related-read-content-show'>\
                  <a href='#'>\
                    <img\
                      class='related-read-pic'\
                      src='"+ item.img +"'\
                      alt=''\
                    />\
                    <p class='related-read-summary'>"+ item.title +"</p>\
                  </a>\
                </div>\
                ")
            })
        }
    })
})